package com.lianzhihui.romeex.utils;

import android.content.Context;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.lianzhihui.romeex.bean.WalletInfoResponse;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.net.ResultData;
import com.lianzhihui.romeex.mvp.view.LoginView;

import io.realm.Realm;

/**
 * Copyright (C), 2019-2019, wase_five
 * FileName: JS2AndroidUtil
 * Author: echo
 * Date: 2019/10/25 18:14
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
public class JS2AndroidUtil implements Realm.Transaction.OnSuccess{
    private Context context;
    private LoginView loginView;
    private int walletType = 1;

    public JS2AndroidUtil(Context context, LoginView loginView) {
        this.context = context;
        this.loginView = loginView;
    }

    public void setWalletType(int walletType){
        this.walletType = walletType;
    }

    // 创建钱包返回数据
    @JavascriptInterface
    public void walletInfoCB(String msg) {
        ResultData resultData = GsonUtil.GsonToBean(msg,ResultData.class);
        if (resultData.getCode()==0){
            try{
                WalletInfoResponse walletInfoResponse = GsonUtil.GsonToBean(resultData.getData(),WalletInfoResponse.class);
                WalletInfo walletInfo = walletInfoResponse.getWalletInfo();
                goWalletPate(walletInfo);
            }catch (Exception e){
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void goWalletPate(WalletInfo walletInfo){
        switch (walletType){
            case 1:
                loginView.setAccountData(walletInfo);
                break;
            case 2:
                loginView.setImportData(walletInfo);
                break;
            case 3:
                loginView.setWalletData(walletInfo);
                break;
        }
    }
    @Override
    public void onSuccess() {

    }
}
